import { computed } from "vue";
import { useStore } from "@/store";
import type { Menu } from "@model/views";

export default function () {
	const store = useStore();

	function getUser<T = any>(key: string): T {
		return store.getters[`user/${key}`];
	}

	function setUser<T = any>(key: string, value?: T) {
		store.dispatch(`user/${key}`, value);
	}
	const token = computed<string>({
		get: () => getUser("token"),
		set: value => {
			setUser("LOGIN", value);
		},
	});

	const permissions = computed<string[]>({
		get: () => getUser("permissions"),
		set: value => {
			setUser("SET_PERMISSIONS", value);
		},
	});

	const menuList = computed<Menu[]>({
		get: () => getUser("menuList"),
		set: value => {
			setUser("SET_MENULIST", value);
		},
	});

	return {
		token,
		permissions,
		menuList,
	};
}
